package com.qq.controller;

import com.qq.model.Customer;
import com.qq.model.House;
import com.qq.model.Orders;
import com.qq.service.CustomerService;
import com.qq.service.HouseService;
import com.qq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/intake")
public class intakeController {
    Set<Customer> customers = new HashSet<>();

    @Autowired
    CustomerService customerService;

    @Autowired
    HouseService houseService;

    @RequestMapping("customer/message")
    String message() {
        return "customer/inMessage";
    }

    @RequestMapping("customer/doMess")
    String doMass(Customer customer, HttpServletRequest request) {
        customers.add(customer);
        request.setAttribute("customers", customers);
        return "customer/list";
    }

    @RequestMapping("alterHouse")
    String store(HttpSession session, HttpServletRequest request) {
        Orders orders = (Orders) session.getAttribute("ORDERS");
        int houseId = orders.getHouseId();
        StringBuilder cid = new StringBuilder();
        List<Customer> Culist = new ArrayList<>(customers);
        for (Customer customer : Culist) {
            customerService.addCustomer(customer);
            Customer cust = customerService.selectCustomer(customer.getIdCard());
            if (Culist.get(0).equals(customer)) {
                cid.append(cust.getId());
            } else {
                cid.append(":").append(cust.getId());
            }
            houseService.updateHouseId(cid.toString(), houseId);
        }
        String state = "入住";
        System.out.println(houseId);
        House house = houseService.findById(houseId);
        System.out.println(house);
        request.setAttribute("house", house);
        System.out.println(house);
        if ("已定".equals(house.getState())) {
            houseService.updateHouseByState(state);
        }
        customers.clear();
        return "house/alterHouse";
    }
}
